package team.group.exam.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import team.group.exam.dao.ITitlesDao;
import team.group.exam.entity.TbQuestionbank;

public class TitlesDaoImpl extends HibernateDaoSupport implements  ITitlesDao{

	private int pageSize;
	/**
	 * 增加
	 */
	@Override
	public void addTitle(TbQuestionbank paperBank) {
		this.getHibernateTemplate().save(paperBank);
	}

	/**
	 * 删除
	 */
	@Override
	public void deleteTitle(TbQuestionbank paperBank) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(paperBank);
	}

	/**
	 * 修改
	 */
	@Override
	public void updateTitle(TbQuestionbank paperBank) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(paperBank);
	}

	/**
	 * 按类型查询
	 */
	@Override
	public List<TbQuestionbank> getByType(String type) {
		// TODO Auto-generated method stub
		List<TbQuestionbank> list = this.getHibernateTemplate().find("from TbQuestionbank where questionType=?", type);
		return list;
	}

	/**
	 * 分页查询所有的
	 */
	@Override
	public List<TbQuestionbank> getAllTitles(int currentPage) {
		System.out.println("当前页：" + currentPage + "  pagesize:" + pageSize);
		// TODO Auto-generated method stub
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		@SuppressWarnings("unchecked")
		List<TbQuestionbank> list = session.createCriteria(TbQuestionbank.class)
		                           .setFirstResult((currentPage-1)*pageSize)
		                           .setMaxResults(pageSize).list();
		session.close();
		return list;
	}
	
	/**
	 * 查询所有的
	 */
	@Override
	public List<TbQuestionbank> getAllTitles() {
		List<TbQuestionbank> list =  this.getHibernateTemplate().find("from TbQuestionbank");
		return list;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 根据id查询
	 */
	@Override
	public TbQuestionbank getTitleById(int id) {
		List<TbQuestionbank> list = this.getHibernateTemplate().find("from TbQuestionbank where titleId=?", id);
		return list.get(0);
	}
	
	/**
	 * 查询总页数
	 */
	@Override
	public int getTotalPage() {
		// TODO Auto-generated method stub
		List<Long> list = this.getHibernateTemplate().find("select count(titleId) from TbQuestionbank");
		int count = Integer.parseInt(list.get(0).toString());
		int totalPage = (count%pageSize) == 0 ? (count/pageSize) : ((count/pageSize)+1);
		return totalPage;
	}

	/**
	 * 查询所有的题目类型
	 */
	@Override
	public List<String> getAllTypes() {
		// TODO Auto-generated method stub
		List<String> list = this.getHibernateTemplate().find("select distinct questionType from TbQuestionbank");
		System.out.println(list.size());
		return list;
	}
   
	@Override
	public int getMaxNo() {
		// TODO Auto-generated method stub
		
	    List list=this.getHibernateTemplate().find("select max(titleId) from TbQuestionbank");
	    return Integer.parseInt(list.get(0).toString());
	}

	@Override
	public boolean getByID(int id) {
		// TODO Auto-generated method stub
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		if(session.get(TbQuestionbank.class, id)!=null){
			session.close();
			return true;
		}
		session.close();
		return false;
	}
}
